<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Setting extends CI_Controller {
	
	function __construct()
    {
		// Call the Model constructor
        parent::__construct();
		
		if($this->session->userdata('login') === FALSE)
			redirect(base_url());
    }
	
	public function profile()
	{
		$this->load->model('model_user');
		
		$id = $this->session->userdata('uid');
		
		if(isset($_POST['data'])){
				$upload = false;
				if (!empty($_FILES) && $_FILES['userfile']['size'] != 0) {
					$upload = true;
					$this->load->helper('string');
					$config['upload_path'] = "./images/profile/";
					$config['allowed_types'] = 'gif|jpg|png|jpeg';
					$config['file_name']	= random_string('alnum', 30);					
					$config['max_size']	= '10240';
					$config['overwrite']  = true;				

					$this->load->library('upload', $config);

					if ( ! $this->upload->do_upload())
					{
						$msg = '{"status":"error","msg":"'.$this->upload->display_errors('','').'"}';
						echo "<script>window.top.respon('$msg');</script>";
						
						return;
					}
					else
					{
						$upload_data = $this->upload->data();
					}
				}

				$data = $_POST['data'];
				
				if(isset($upload_data)){
					if($data['profile_image'] != ''){
						//menghapus file
						if($data['profile_image'] != "default-128.png")
							unlink($upload_data['file_path'].$data['profile_image']);
					}
					
					$data['profile_image'] = $upload_data['orig_name'];
					
					$this->session->set_userdata('pp',$upload_data['orig_name']);
					
					unset($config);
					$config['image_library'] = 'gd2';
					$config['source_image']	= $upload_data['full_path'];
					$config['maintain_ratio'] = TRUE;
					$config['width']	 = 128;
					$config['height']	= 128;

					$this->load->library('image_lib', $config); 
					
					if ( ! $this->image_lib->resize())
					{
						echo $this->image_lib->display_errors();
					}
					
					$this->image_lib->clear();
					
					// unset($config);
					// $config['image_library'] = 'gd2';
					// $config['source_image']	= $upload_data['full_path'];
					// $config['maintain_ratio'] = TRUE;
					// $config['create_thumb'] = TRUE;
					// $config['thumb_marker'] = '-50';
					// //$config['new_image'] = "./images/profile/".$upload_data['raw_name'].'-50.'.$upload_data['file_ext'];
					// $config['width']	 = 50;
					// $config['height']	= 50;
					
					// $this->load->library('image_lib', $config);
					
					// if ( ! $this->image_lib->resize())
					// {
						// echo $this->image_lib->display_errors();
					// }
					
					// unset($config);
					// $config['image_library'] = 'gd2';
					// $config['source_image']	= $upload_data['full_path'];
					// $config['maintain_ratio'] = TRUE;
					// $config['create_thumb'] = TRUE;
					// $config['thumb_marker'] = '-32';
					// $config['width']	 = 32;
					// $config['height']	= 32;

					// $this->load->library('image_lib', $config); 

					// if ( ! $this->image_lib->resize())
					// {
						// echo $this->image_lib->display_errors();
					// }

				}
				
				$this->model_user->edit($id,$data);

				// /*
				 // * ini digunakan untuk memberitahukan prosesnya berhasil atau tidak
				 // * data ini dalam bentuk json
				 // * status berisikan success atau error
				 // */
				$msg = '{"status":"success","msg":"Profil berhasil disimpan","url":"setting/profile.html"}';
				echo "<script>window.top.respon('$msg');</script>";
				return;
			}
		
		$data['user'] = $this->model_user->get_by($id);
		$sub['sub'] = $this->load->view('member/pengaturan/profile',$data,TRUE);
		$view['content'] = $this->load->view('member/frame',$sub,TRUE);
		$this->load->view('index',$view);
	}
	
	public function kota($id=0)
	{
		$this->load->model('model_kota');
		
		if(isset($_POST['data'])){
			$data = $_POST['data'];
			
			$url = preg_replace('~[^\\pL0-9_]+~u', '-', str_replace(" ","-",$data['nama']));
			$url = trim($url, "-");
			$url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
			$url = strtolower($url);
			$url = preg_replace('~[^-a-z0-9_]+~', '', $url);

			$data['permalink'] = $url;
			
			if($id==0){
				$data['user_id'] = $this->session->userdata('uid');
				$data['created'] = date('Y-m-d H:i:s');
				$this->model_kota->add($data);
			}
			/*
			 * ini digunakan untuk memberitahukan prosesnya berhasil atau tidak
			 * data ini dalam bentuk json
			 * status berisikan success atau error
			 * url berisikan, setelah berhasil, nanti akan diredirect kemana.
			 */
			echo '{"status":"success","msg":"Data berhasil disimpan"}';
			
			return;
		}
		
		$data['kota'] = $this->model_kota->get_by($id);
		$this->load->view('member/pengaturan/kota',$data);
	}
	
	public function password()
	{
		$this->load->model('model_user');
		
		$id = $this->session->userdata('uid');
		
		if(isset($_POST['password_lama'])){
			$user = $this->model_user->get_by($id);
			$check = false;
			//var_dump($user->result_array());
			foreach($user->result_array() as $row){
				if(md5(sha1($_POST['password_lama'])) == $row['password']){
					$check = true;
				}
			}
			
			if($check){
				$data['password'] = md5(sha1($_POST['password_baru']));
				
				$this->model_user->edit($id,$data);
				
				echo $msg = '{"status":"success","msg":"Password berhasil disimpan"}';
				return;
			}else{
				echo $msg = '{"status":"error","msg":"Password Anda salah"}';
				return;
			}
		}
		
		$sub['sub'] = $this->load->view('member/pengaturan/password','',TRUE);
		$view['content'] = $this->load->view('member/frame',$sub,TRUE);
		$this->load->view('index',$view);
	}
	
	public function notif()
	{
		$this->load->model('model_user');
		
		$id = $this->session->userdata('uid');
		
		if(isset($_POST['data'])){
			
			$data = $_POST['data'];
			
			$this->model_user->edit($id,$data);
				
			echo $msg = '{"status":"success","msg":"Notifikasi berhasil disimpan"}';
			return;
		}
		
		$data['user'] = $this->model_user->get_by($id);
		$sub['sub'] = $this->load->view('member/pengaturan/notif',$data,TRUE);
		$view['content'] = $this->load->view('member/frame',$sub,TRUE);
		$this->load->view('index',$view);
	}
}
